package com.example.itshome.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.example.itshome.R;

import java.util.ArrayList;
import java.util.List;

public class BottomRailView extends LinearLayout implements View.OnClickListener{

    private List<Tab> mTabs;
    private List<View> mTabViews;
    private OnTabCheckListener onTabCheckListener;


    public BottomRailView(Context context) {
        super(context);
        init();
    }

    public BottomRailView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BottomRailView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BottomRailView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    public void onClick(View v) {
        int position= (int) v.getTag();
        if(position<0||position>=mTabViews.size()){
            position=0;
        }
        if(onTabCheckListener!=null){
            onTabCheckListener.onTabSelected(v,position);
        }
        updatePosition(position);
    }

    public void updatePosition(int position){
        for (int i=0;i<mTabViews.size();i++) {
            View mTabView=mTabViews.get(i);
            ImageView imageView= mTabView.findViewById(R.id.tab_content_image);
            if(i==position){
                imageView.setImageResource(mTabs.get(i).mIconPressedResId);
            }else{
                imageView.setImageResource(mTabs.get(i).mIconNormalResId);
            }
        }
    }

    public void addTab(Tab tab){
        View view= LayoutInflater.from(getContext()).inflate(R.layout.bottom_rail_tab,null);
        TextView textView=view.findViewById(R.id.tab_content_text);
        ImageView imageView=view.findViewById(R.id.tab_content_image);
        textView.setText(tab.text);
        imageView.setImageResource(tab.mIconNormalResId);

        view.setTag(mTabViews.size());
        view.setOnClickListener(this);

        mTabViews.add(view);
        mTabs.add(tab);

        addView(view);

    }

    void init(){
        setGravity(Gravity.TOP);
        setOrientation(HORIZONTAL);
        mTabs=new ArrayList<>();
        mTabViews=new ArrayList<>();
    }

    public static class Tab{
        private int mIconNormalResId;
        private int mIconPressedResId;
        private String text;

        public Tab setmIconNormalResId(int mIconNormalResId) {
            this.mIconNormalResId = mIconNormalResId;
            return this;
        }

        public Tab setmIconPressedResId(int mIconPressedResId) {
            this.mIconPressedResId = mIconPressedResId;
            return this;
        }

        public Tab setText(String text) {
            this.text = text;
            return this;
        }
    }

    public void setOnTabCheckListener(OnTabCheckListener listener){
        onTabCheckListener=listener;
    }

    public interface  OnTabCheckListener{
        public void onTabSelected(View v,int position);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mTabViews!=null){
            mTabViews.clear();
        }
        if(mTabs!=null){
            mTabs.clear();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // 调整每个Tab的大小
        for (View view : mTabViews) {
            int width = getResources().getDisplayMetrics().widthPixels / (mTabs.size());
            LayoutParams params = new LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);

            view.setLayoutParams(params);
        }

    }
}
